home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_092 / uuencode / uuencode.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  165 lines

  1. /* #ifndef lint
  2. static char sccsid[] = "@(#)uuencode.c  5.3-1 (Berkeley) 1/22/85";
  3. #endif */
  4.  
  5. /* Written by Mark Horton */
  6. /* Modified by ajr (Alan J Rosenthatl,flaps@utcsri.UUCP) to use checksums */
  7. /* Modified by fnf (Fred Fish,well!fnf) to use Keith Pyle's suggestion for
  8.    compatibility */
  9. /* Modified by bcn (Bryce Nesbitt,ucbvax!cogsci!bryce) to fix a very
  10.    misleading error message on the Amiga port, enable CTRL-C for LATTICE,
  11.    and add a transparant file size trailer for later check. */
  12.  
  13. /*
  14.  * uuencode >outfile infile name
  15.  *
  16.  * Encode a file so it can be mailed to a remote system.  This version
  17.  * transparantly adds line checksums and a file size for sanity checks.
  18.  *
  19.  */
  20.  
  21. #include <stdio.h>
  22.  
  23. #ifdef AMIGA
  24. #define AMIGA_LATTICE        /* Set for Amiga Lattice C */
  25. #define MCH_AMIGA
  26. #define MPU68000
  27. #endif
  28.  
  29. #ifdef unix
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #endif
  33.  
  34. #define SUMSIZE 64  /* 6 bits */
  35. /* ENC is the basic 1 character encode function to make a char printing */
  36. /* Each output character represents 6 bits of input */
  37. #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  38. long    totalsize=0;    /* Used to count the file size because ftell() does
  39.                not return sane results for pipes */
  40.  
  41. main(argc, argv)
  42. char **argv;
  43. {
  44.     FILE *in;
  45.     int mode;
  46. #ifdef unix
  47.     struct stat sbuf;
  48. #endif
  49. #ifdef AMIGA_LATTICE_OBSOLETE
  50.     extern int Enable_Abort;    /* Enable CTRL-C for Lattice */
  51.     Enable_Abort=1;
  52. #endif
  53.  
  54.     /* optional 1st argument */
  55.     if (argc > 2) {
  56.         if ((in = fopen(argv[1], "r")) == NULL) {
  57.             fprintf(stderr, "ERROR: can't find %s\n", argv[1]);
  58.             fprintf(stderr, "USAGE: uuencode >outfile infile name\n");
  59.             exit(10);
  60.         }
  61.         argv++; argc--;
  62.     } else
  63.         in = stdin;
  64.  
  65.     if (argc != 2) {
  66.         fprintf(stderr, "USAGE: uuencode >outfile infile name\n");
  67.         exit(11);
  68.     }
  69.  
  70. #ifdef unix
  71.     /* figure out the input file mode */
  72.     fstat(fileno(in), &sbuf);
  73.     mode = sbuf.st_mode & 0777;
  74. #else
  75.     mode = 0644;        /* Default permissions */
  76. #endif
  77.  
  78.     printf("\nbegin %o %s\n", mode, argv[1]);
  79.  
  80.     encode(in, stdout);
  81.  
  82.     printf("end\n");
  83.     printf("size %ld\n",totalsize);
  84.     exit(0);
  85. }
  86.  
  87. /*
  88.  * copy from in to out, encoding as you go along.
  89.  */
  90. encode(in, out)
  91. FILE *in;
  92. FILE *out;
  93. {
  94. #ifndef unix
  95. extern errno;
  96. #endif
  97.     char buf[80];
  98.     int i, n, checksum;
  99.  
  100.     for (;;) {
  101.         /* 1 (up to) 45 character line */
  102.         n = fr(in, buf, 45);
  103.         putc(ENC(n), out);
  104.  
  105.         checksum = 0;
  106.         for (i=0; i<n; i += 3)
  107.             checksum = (checksum+outdec(&buf[i], out)) % SUMSIZE;
  108.  
  109.         putc(ENC(checksum), out);
  110.         putc('\n', out);
  111.  
  112. #ifndef unix
  113.         /* Error checking under UNIX?? You must be kidding! */
  114.         if (errno) {
  115.             fprintf(stderr, "ERROR: error writing to output\n");
  116.             exit(12);
  117.             }
  118. #endif
  119.         if (n <= 0)
  120.             break;
  121.     }
  122. }
  123.  
  124. /*
  125.  * output one group of 3 bytes, pointed at by p, on file f.
  126.  * return the checksum increment.
  127.  */
  128. int outdec(p, f)
  129. char *p;
  130. FILE *f;
  131. {
  132.     int c1, c2, c3, c4;
  133.  
  134.     c1 = *p >> 2;
  135.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  136.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  137.     c4 = p[2] & 077;
  138.     putc(ENC(c1), f);
  139.     putc(ENC(c2), f);
  140.     putc(ENC(c3), f);
  141.     putc(ENC(c4), f);
  142.  
  143.     return((p[0]+p[1]+p[2]) % SUMSIZE);
  144. }
  145.  
  146. /* fr: like read but stdio */
  147. int
  148. fr(fd, buf, cnt)
  149. FILE *fd;
  150. char *buf;
  151. int cnt;
  152. {
  153.     int c, i;
  154.  
  155.     for (i=0; i<cnt; i++) {
  156.         c = getc(fd);
  157.         if (c == EOF)
  158.             return(i);
  159.         totalsize++;
  160.         buf[i] = c;
  161.     }
  162.     return (cnt);
  163. }
  164.  
  165.